package cn.szzsi.common.model;

import cn.szzsi.common.model.base.BaseTocashBank;
import com.jfinal.kit.StrKit;

import java.util.Date;
import java.util.List;


/**
 * 体现银行卡管理
 * Created by yuanzheng on 2016/11/29.
 */
//@TableBind(tableName = "b_tocash_bank", pkName = "id")
public class TocashBank extends BaseTocashBank<TocashBank> {
    public static final TocashBank dao=new TocashBank().dao();

    public static List<TocashBank> getByMid(String member_id){
        return dao.find("select * from mall_tocash_bank where member_id=? order by createDate desc ",member_id);
    }

    /**
     * 会员id
     * @param member_id
     * @return
     */
    public static TocashBank getByMemberId(String member_id){
        return dao.findFirst("select * from mall_tocash_bank where member_id=? order by createDate desc ",member_id);
    }

    public static final TocashBank saveCashingBank(String member_id, String name, String cardNumber, String cardCount, String cardAddress, String idNumber,String aliPayAccount,String weChatAccount){
        TocashBank tocashBank=getByMemberId(member_id);
        if(tocashBank!=null){
            tocashBank.setName(name);
            tocashBank.setCardNumber(cardNumber);
            tocashBank.setCardCount(cardCount);
            if(StrKit.notBlank(cardAddress)){
                tocashBank.setCardAddress(cardAddress);
            }
            if(StrKit.notBlank(idNumber)){
                tocashBank.setIdNumber(idNumber);
            }
            tocashBank.setMemberId(member_id);
            tocashBank.setCreateDate(new Date());
            if(StrKit.notBlank(aliPayAccount)){
                tocashBank.setAliPayAccount(aliPayAccount);
            }
            if(StrKit.notBlank(weChatAccount)){
                tocashBank.setWeChatAccount(weChatAccount);
            }
            tocashBank.update();
            return tocashBank;
        }else{
            TocashBank newTocashBank=new TocashBank();
            newTocashBank.setName(name);
            newTocashBank.setCardNumber(cardNumber);
            newTocashBank.setCardCount(cardCount);
            if(StrKit.notBlank(cardAddress)){
                newTocashBank.setCardAddress(cardAddress);
            }
            if(StrKit.notBlank(idNumber)){
                newTocashBank.setIdNumber(idNumber);
            }
            newTocashBank.setMemberId(member_id);
            newTocashBank.setCreateDate(new Date());
            if(StrKit.notBlank(aliPayAccount)){
                newTocashBank.setAliPayAccount(aliPayAccount);
            }
            if(StrKit.notBlank(weChatAccount)){
                newTocashBank.setWeChatAccount(weChatAccount);
            }
            newTocashBank.save();
            return  newTocashBank;
        }
    }

}
